package com.cws4cloud.cws.mall.cms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.mall.cms.entity.News;
import com.cws4cloud.cws.mall.cms.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 内容管理
 *
 * @author zhulj
 * @date 2021-04-20 17:21:31
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/news" )
@Api(value = "news", tags = "内容管理管理")
public class NewsController {

    private final NewsService newsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param news 内容管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_news_get')" )
    public R getNewsPage(Page page, News news) {
        return R.ok(newsService.page(page, Wrappers.query(news)));
    }


    /**
     * 通过id查询内容管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_news_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
    	News news = newsService.getById(id);
    	JSONArray jsonArray = JSONArray.parseArray(news.getFile());
    	news.setFileObject(jsonArray);
        return R.ok(newsService.getById(id));
    }

    /**
     * 新增内容管理
     * @param news 内容管理
     * @return R
     */
    @ApiOperation(value = "新增内容管理", notes = "新增内容管理")
    @SysLog("新增内容管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_news_add')" )
    public R save(@RequestBody News news) {
    	if (news.getFileObject()!=null) {
			String file = JSON.toJSONString(news.getFileObject());
			news.setFile(file);
		}
        return R.ok(newsService.save(news));
    }

    /**
     * 修改内容管理
     * @param news 内容管理
     * @return R
     */
    @ApiOperation(value = "修改内容管理", notes = "修改内容管理")
    @SysLog("修改内容管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_news_edit')" )
    public R updateById(@RequestBody News news) {
		if (news.getFileObject()!=null) {
			String file = JSON.toJSONString(news.getFileObject());
			news.setFile(file);
		}
        return R.ok(newsService.updateById(news));
    }

    /**
     * 通过id删除内容管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除内容管理", notes = "通过id删除内容管理")
    @SysLog("通过id删除内容管理" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_news_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(newsService.removeById(id));
    }

	/**
	 * 通过栏目查询新闻总数
	 * @param classlistId
	 * @return
	 */
	@ApiOperation(value = "通过栏目查询新闻总数", notes = "通过栏目查询新闻总数")
	@GetMapping("getCountByClass/{classlistId}")
	public R getCountByClass(@PathVariable("classlistId" ) Integer classlistId) {
		return R.ok(newsService.count(Wrappers.<News>lambdaQuery().eq(News::getClasslistId,classlistId)));
	}


	/**
	 * 通过栏目分组查询新闻总数
	 * @param
	 * @return
	 */
	@ApiOperation(value = "通过栏目分组查询新闻总数", notes = "通过栏目分组查询新闻总数")
	@GetMapping("getCountGroupByClass")
	public R getCountGroupByClass(@RequestParam("siteId") Integer siteId) {
		return R.ok(newsService.getNewsCountGroupByClass(siteId));
	}
}
